Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
I got an Error: Objects are not valid as a React child (found: [object Promise])

I am kind of new to React. And I got this Error: Objects are not valid as a React child (found: [object Promise]). And I am not sure how to fix this issue. Can you guys take a look? Thanks!

Here's my code.

import React, { Component } from "react";
import "./App.css";

async function randomVerse() {
  const url = "https://labs.bible.org/api/?passage=random&type=json";
  const passage = await fetch(url);
  const passageJson = await passage.json();

  return passageJson;
}

const displayRandomVerse = async () => {
  let result = await randomVerse();
  let verse = "";
  verse += result[0].text;
  return verse;
};

const returnVerse = displayRandomVerse();

class App extends Component {
  constructor() {
    super();
    this.state = {
      verse: null
    };
  }

  componentDidMount() {
    this.setState({verse: returnVerse});
  };

  render() {
    return (
      <div className="App">
        <h1>Good Morning, XXX!</h1>
        <h3>Bible verse : {this.state.verse} </h3>
      </div>
    );
  }
}

export default App;

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You have to set await in const returnVerse = await displayRandomVerse();. This should give you the value

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to fetch and await in your componentDidMount method

async function randomVerse() {
      const url = "https://labs.bible.org/api/?passage=random&type=json";
      const passage = await fetch(url);
      const passageJson = await passage.json();
    
      return passageJson;
    }
    
    const displayRandomVerse = async () => {
      let result = await randomVerse();
      let verse = "";
      verse += result[0].text;
      return verse;
    };
    
    
    class App extends Component {
      constructor() {
        super();
        this.state = {
          verse: null
        };
      }
    
      async componentDidMount() {  //Notice changes in this section
        this.setState({verse: await displayRandomVerse()});
      };
    
      render() {
        return (
          <div className="App">
            <h1>Good Morning, XXX!</h1>
            <h3>Bible verse : {this.state.verse} </h3>
          </div>
        );
      }
    }
    
    export default App;
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda